﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Product;
using Base;

public partial class member_Trade_PurchaseManage : nspBase.MemberBasePge
{
   public string verify="1";
    public string pageStr = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CheckCookies())
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Verify"] != null && Request.QueryString["Verify"] != "")
                {
                    verify = Request.QueryString["Verify"].ToString();
                }
                Bind();
                ViewReate();
                Viewxiugai();
                Viewxuanxiang();
            }
        }
        else
            Response.Redirect("../../login/login.aspx");
    }

    private void Bind()
    {
        try
        {
            int Userid = int.Parse(GetCookie("UserID"));
            if (Userid > 0)
            {
                int PageIndex = 0;
                if (HttpContext.Current.Request.QueryString["page"] != null)
                    PageIndex = Convert.ToInt32(Request.QueryString["Page"].ToString());
                if (PageIndex <= 0) PageIndex = 1;
                string strWhere = " UserID=" + Userid;
                if (Request.QueryString["Verify"] != null && Request.QueryString["Verify"] != "")
                {

                    if (Request.QueryString["Verify"] == "4")//过期信息
                    {
                        strWhere += " and (datediff(hh,EndDate,getdate())>=0) and Verify =1";
                    }
                    else if (Request.QueryString["Verify"] == "2")//审核未通过信息
                    {
                        strWhere += " and Verify =";
                        strWhere += Request.QueryString["Verify"];
                    }
                    else if (Request.QueryString["Verify"] == "3")//审核中信息
                    {
                        strWhere += " and Verify =";
                        strWhere += Request.QueryString["Verify"];
                       // this.pldelete.Visible = false;
                    }
                    else //已发布上网
                    {
                        strWhere += " and (datediff(hh,EndDate,getdate())<=0) and Verify =";
                        strWhere += Request.QueryString["Verify"];
                    }
                    verify = Request.QueryString["Verify"];
                }
                else //默认进入已发布上网
                {
                    strWhere += " and Verify =1 and (datediff(hh,EndDate,getdate())<0)";

                }
                DataTable Lists = PageList.GetDataTable(strWhere, "C_Purchase", 1, "AddDate", "*", 10, PageIndex, 0);
                dlstPurchase.DataSource = Lists.DefaultView;
                dlstPurchase.DataBind();
                //this.Pagin.Text = PageList.GoToPager(PageIndex, 10, "PurchaseManage.aspx?Verify=" + verify + "&page", strWhere, "C_Purchase");
                pageStr = PageList.GoToPager(PageIndex, 10, "PurchaseManage.aspx?Verify=" + verify + "&page", strWhere, "C_Purchase");
                //btnDelete.Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
                
                ViewReate();
                Viewxiugai();
                Viewxuanxiang();
               // SetBackColor();
            }
            else
                Response.Redirect("../login/login.aspx");
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }

    private void ViewReate()
    {
        if (!String.IsNullOrEmpty(Request.QueryString["Verify"]))
        {
            if ((Request.QueryString["Verify"].Equals("4")))
            {
                for (int i = 0; i < dlstPurchase.Items.Count; i++)
                {
                    if (((LinkButton)(dlstPurchase.Items[i].FindControl("chongfa"))).Enabled == false)
                    {
                        ((LinkButton)(dlstPurchase.Items[i].FindControl("chongfa"))).Enabled = true;
                    }
                }
            }
            else if ((Request.QueryString["Verify"].Equals("1")))
            {
                for (int i = 0; i < dlstPurchase.Items.Count; i++)
                {
                    if (((LinkButton)(dlstPurchase.Items[i].FindControl("chongfa"))).Enabled == false)
                    {
                        ((LinkButton)(dlstPurchase.Items[i].FindControl("chongfa"))).Enabled = true;
                    }
                }
            }
            else
            {
                for (int i = 0; i < dlstPurchase.Items.Count; i++)
                {
                    if (((LinkButton)(dlstPurchase.Items[i].FindControl("chongfa"))).Enabled == true)
                    {
                        ((LinkButton)(dlstPurchase.Items[i].FindControl("chongfa"))).Enabled = false;
                    }
                }
            }
        }
    }
    /// <summary>
    /// 修改
    /// </summary>
    private void Viewxiugai()
    {
        if (!String.IsNullOrEmpty(Request.QueryString["Verify"]))
        {
            if (Request.QueryString["Verify"].Equals("3"))
            {
                for (int i = 0; i < dlstPurchase.Items.Count; i++)
                {
                    if (((LinkButton)(dlstPurchase.Items[i].FindControl("xiugai"))).Enabled == true)
                    {
                        ((LinkButton)(dlstPurchase.Items[i].FindControl("xiugai"))).Enabled = false;
                    }
                }
            }
            else
            {
                for (int i = 0; i < dlstPurchase.Items.Count; i++)
                {
                    if (((LinkButton)(dlstPurchase.Items[i].FindControl("xiugai"))).Enabled == false)
                    {
                        ((LinkButton)(dlstPurchase.Items[i].FindControl("xiugai"))).Enabled = true;
                    }
                }
            }
        }
    }
    /// <summary>
    /// 选项
    /// </summary>
    private void Viewxuanxiang()
    {

        //if (!String.IsNullOrEmpty(Request.QueryString["Verify"]))
        //{
        //    if (Request.QueryString["Verify"].Equals("3"))
        //    {
        //        for (int i = 0; i < dlstPurchase.Items.Count; i++)
        //        {
        //            if (((CheckBox)(dlstPurchase.Items[i].FindControl("CheckBox1"))).Enabled == true)
        //            {
        //                ((CheckBox)(dlstPurchase.Items[i].FindControl("CheckBox1"))).Enabled = false;
        //            }
        //        }
        //    }
        //    else
        //    {
        //        for (int i = 0; i < dlstPurchase.Items.Count; i++)
        //        {
        //            if (((CheckBox)(dlstPurchase.Items[i].FindControl("CheckBox1"))).Enabled == false)
        //            {
        //                ((CheckBox)(dlstPurchase.Items[i].FindControl("CheckBox1"))).Enabled = true;
        //            }
        //        }
        //    }
        //}
    }
    /// <summary>
    /// 取得短日期
    /// </summary>
    /// <param name="dateStr"></param>
    /// <returns></returns>
    public string GetShortDate(string dateStr)
    {
        if (dateStr != string.Empty)
            return Convert.ToDateTime(dateStr).ToShortDateString();
        else
            return string.Empty;
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string[] ids = CheckedIDS.Value.Split(',');
        try
        {
            for (int i = 0; i < ids.Length; i++)
            {
                int UserID = Convert.ToInt32(ids[i]);
                IPurchase RM = new RPurchase();
                int m = RM.Delete(UserID);
            }
            Bind();
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }

    public string GetImageUrl(string Url)
    {
        if (Url != "")
        {
            return Url;
        }
        else
        {
            return "../../Images/System/wutu.gif";
        }
        return "../../Images/System/wutu.gif";
    }
    /// <summary>
    /// 重发供应信息功能
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnChongfa(object sender, CommandEventArgs e)
    {
        try
        {
            String str = DateTime.Now.AddDays(10).ToString();
            String strID = e.CommandArgument.ToString();
            String endtime = DateTime.Now.ToString();
            Solely.Other.UpOnlyDate("C_Purchase", "EndDate='" + str + "',AddDate='" + endtime + "',StartDate='" + endtime + "'", "id=" + strID);
            Bind();
        }
        catch (Exception ex)
        {
            lblmess.InnerHtml = "服务器繁忙,请刷新再试";
        }
    }
    protected void xiugai_Command(object sender, CommandEventArgs e)
    {
        String strid = e.CommandArgument.ToString();
        Response.Write("<script>location.replace('PurchaseEdit.aspx?id= " + strid + "');</script>");
    }
}
